package com.teachoa.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.teachoa.util.ConvertUtil;

public class PageTag extends TagSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4564861302612155867L;
	
	/**
	 * 
		<ul class="pagination">
	        <li><a href="javascript:;" onclick="turnpage(1)">上一页</a></li>
	        <li class="active">
	            <a href="#">1</a>
	        </li>
	        <li><a href="#">2</a></li>
	        <li><a href="#">3</a></li>
	        <li><a href="#">4</a></li>
	        <li><a href="#">下一页</a></li>
	    </ul>
	 * 
	 */
	
	/**
	 * {pageNo:xxx,total:xxx,jsName:xxx}
	 * 
	 */
	
	
	/**
	 * 1 2345
	 * 12 345
	 * 123 45
	 * 1234 5
	 * 456 78
	 * 567 89
	 * ....
	 * 10 11 12 13 14
	 * 11 12 13 14 15
	 */
	
	/*private void test(int pageNo, int total) {
		//int totalPage = 19;
		if(pageNo<=5) System.out.println("1,2,3,4,5");
		if(pageNo>5&&pageNo<total-5) System.out.println((pageNo-2)+","+(pageNo-1)+","+pageNo+","+(pageNo+1)+","+(pageNo+2));
		if(pageNo>=total-5) System.out.println((total-4)+","+(total-3)+","+(total-2)+","+(total-1)+","+total);
	}*/
	
	/*public static void main(String[] args) {
		new PageTag().test(6, 9);
	}*/
	
	private int pageNumber;
	private int totalRow;
	private int totalPage;
	private int width;
	
	private void init() {
		System.err.println("pageNumber="+pageNumber);
		System.err.println("totalRow="+totalRow);
		System.err.println("totalPage="+totalPage);
	}
	
	private String getPageHtml() {
		StringBuilder sb = new StringBuilder();
		sb.append("<div class='col-md-'"+width+"' center-block'><ul class='pagination'>")
		.append(createOuter("首页", ""));
		if(pageNumber!=1) sb.append(createOuter("上一页", ""));
		if(pageNumber<=5&&totalPage>=5) sb.append(createInner(1, 6));
		else if(pageNumber<=5&&totalPage<=5) sb.append(createInner(1, totalPage+1));
		else if(pageNumber>5&&pageNumber<totalPage-5) sb.append(createInner(pageNumber-2, totalPage+3));
		else if(pageNumber>=totalPage-5) sb.append(createInner(totalPage-4, totalPage+1));
		if(pageNumber!=totalPage) sb.append(createOuter("下一页", ""));
		sb.append(createOuter("末页", "")).append("</ul></div>");
		return sb.toString();
	}
	
	private String createOuter(Object desc, Object clazz) {
		int page = 0;
		if(desc.equals("首页")) page = 1;
		else if(desc.equals("上一页")) page = pageNumber==1?1:pageNumber-1;
		else if(desc.equals("下一页")) page = pageNumber==totalPage?totalPage:pageNumber+1;
		else if(desc.equals("末页")) page = totalPage;
		else page = ConvertUtil.toInt(desc);
		return "<li><a href='javascript:;' "+clazz+" onclick='turnpage(\""+page+"\");'>"+desc+"</a></li>";
	}
	
	private String createInner(int start, int end) {
		StringBuilder sb = new StringBuilder();
		for(int i=start;i<end;i++) {
			sb.append(createOuter(i, i==pageNumber?"class='active'":""));
		}
		return sb.toString();
	}

	@Override
	public int doStartTag() throws JspException {
		try {
			init();
			pageContext.getOut().write(getPageHtml());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public void setWidth(int width) {
		this.width = width;
	}

	public static void main(String[] args) {
		PageTag pt = new PageTag();
		//pt.setMap(Maps.newHashMap("pageNo", 3, "total", 15, "js", "page"));
		pt.init();
		System.out.println(pt.getPageHtml());
	}

}
